using System;
using System.Collections.Generic;
using System.Text;

namespace CodeFx
{
    public static class StringHelper
    {

        public static string IsNull(string _string)
        {
            return IsNull(_string, string.Empty);
        }

        public static string IsNull(string _string, string _Default)
        {
            if (string.IsNullOrEmpty(_string))
                return _Default;
            else
                return _string;
        }
        public static string CheckAndTrimString(string paramValue, string paramName)
        {
            return CheckAndTrimString(paramValue, paramName, true);
        }

        public static string CheckAndTrimString(string paramValue, string paramName, bool throwIfNull)
        {
            if (paramValue == null)
            {
                if (throwIfNull)
                {
                    throw new ArgumentNullException(paramName);
                }
                return null;
            }
            return paramValue.Trim();
        }
        
        public static string CheckAndTrimString(string paramValue, string paramName, bool throwIfNull, int lengthToCheck)
        {
            if (paramValue == null)
            {
                if (throwIfNull)
                {
                    throw new ArgumentNullException(paramName);
                }
                return null;
            }
            string text1 = paramValue.Trim();
            if (text1.Length == 0)
            {
                throw new ArgumentException("PersonalizationProviderHelper_TrimmedEmptyString");
            }
            if ((lengthToCheck > -1) && (text1.Length > lengthToCheck))
            {
                throw new ArgumentException("StringUtil_Trimmed_String_Exceed_Maximum_Length");
            }
            return text1;
        }

        public static bool Equals(string s1, string s2)
        {
            if (s1 == s2)
            {
                return true;
            }
            if (string.IsNullOrEmpty(s1) && string.IsNullOrEmpty(s2))
            {
                return true;
            }
            return false;
        }

        public static bool EqualsIgnoreCase(string s1, string s2)
        {
            if (string.IsNullOrEmpty(s1) && string.IsNullOrEmpty(s2))
            {
                return true;
            }
            if (string.IsNullOrEmpty(s1) || string.IsNullOrEmpty(s2))
            {
                return false;
            }
            if (s2.Length != s1.Length)
            {
                return false;
            }
            return (0 == string.Compare(s1, 0, s2, 0, s2.Length, StringComparison.OrdinalIgnoreCase));
        }

        public static bool EqualsIgnoreCase(string s1, int index1, string s2, int index2, int length)
        {
            return (string.Compare(s1, index1, s2, index2, length, StringComparison.OrdinalIgnoreCase) == 0);
        }

        public static string[] ObjectArrayToStringArray(object[] objectArray)
        {
            if (objectArray == null)
                return new string[0];
            string[] textArray1 = new string[objectArray.Length];
            objectArray.CopyTo(textArray1, 0);
            return textArray1;
        }

        public static string Replace(string s, char c1, char c2)
        {
            if (string.IsNullOrEmpty(s))
                return string.Empty;
            int num1 = s.IndexOf(c1);
            if (num1 < 0)
            {
                return s;
            }
            return s.Replace(c1, c2);
        }

        public static bool StringArrayEquals(string[] a, string[] b)
        {
            if ((a == null) != (b == null))
            {
                return false;
            }
            if (a != null)
            {
                int num1 = a.Length;
                if (num1 != b.Length)
                {
                    return false;
                }
                for (int num2 = 0; num2 < num1; num2++)
                {
                    if (a[num2] != b[num2])
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        public static bool StringEndsWith(string s, char c)
        {
            if (string.IsNullOrEmpty(s))
                return false;
            int num1 = s.Length;
            return (s[num1 - 1] == c);
        }

        public static bool StringEndsWithIgnoreCase(string s1, string s2)
        {
            if (string.IsNullOrEmpty(s1) || string.IsNullOrEmpty(s2))
                return false;
            int num1 = s1.Length - s2.Length;
            if (num1 < 0)
            {
                return false;
            }
            return (0 == string.Compare(s1, num1, s2, 0, s2.Length, StringComparison.OrdinalIgnoreCase));
        }

        public static bool StringStartsWith(string s, char c)
        {
            if (s.Length != 0)
            {
                return (s[0] == c);
            }
            return false;
        }

        public static bool StringStartsWithIgnoreCase(string s1, string s2)
        {
            if (string.IsNullOrEmpty(s1) || string.IsNullOrEmpty(s2))
            {
                return false;
            }
            if (s2.Length > s1.Length)
            {
                return false;
            }
            return (0 == string.Compare(s1, 0, s2, 0, s2.Length, StringComparison.OrdinalIgnoreCase));
        }


    }
}
